﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuantitativeInvestment.Bean;
namespace QuantitativeInvestment.Model
{
    class PriceMomentumModel:Model
    {

        public PriceMomentumModel()
        {
            this.name = "价格动量模型";
            Parameter p = new Parameter("观察时间", 30);
            this.paraList.Add(p.name,p);

            p = new Parameter("静止期", 0);
            this.paraList.Add(p.name, p);

            p = new Parameter("持有时间", 30);
            this.paraList.Add(p.name, p);



            p = new Parameter("排序", "低");
            p.type = "enum";
            p.enumList.Add("低");
            p.enumList.Add("高");
            this.paraList.Add(p.name, p);

            p = new Parameter("是否比例", "是");
            p.type = "enum";
            p.enumList.Add("是");
            p.enumList.Add("否");
            this.paraList.Add(p.name, p);

            p = new Parameter("范围开始", 0);
            this.paraList.Add(p.name, p);

            p = new Parameter("范围结束", 30);
            this.paraList.Add(p.name, p);

  

              
        }
        public override List<TradingStock> buy(List<TradingStock> stockList,string tradingDate)
        {
            List<TradingStock> selectedStocks = new List<TradingStock>();


            
            int observePeriod =Int32.Parse( this.paraList["观察时间"].value.ToString());
            //当前一期的时间
            int currentPosition = this.container.tradingDates.IndexOf(tradingDate);
            int coolPeriod =Int32.Parse(this.paraList["静止期"].value.ToString());
            string startDate;
            string endDate;

            //如果超过时间范围就不返回数值
            if (currentPosition - observePeriod - coolPeriod < 0)
                return selectedStocks;
            else
            {
                startDate = this.container.tradingDates[currentPosition - observePeriod - coolPeriod];
                endDate = this.container.tradingDates[currentPosition - coolPeriod];
            }

            List<TradingStock> comparedStocks = new List<TradingStock>();
            foreach (TradingStock tradingStock in stockList)
            {
         
                Stock currentStock = this.container.stocks[tradingStock.code];
                int initialStartPosition = currentStock.tradingDateList.IndexOf(currentStock.startDate);
                int stockStartPosition = currentStock.tradingDateList.IndexOf(startDate);

                int stockEndPosition = currentStock.tradingDateList.IndexOf(endDate);

                //如果之前没有数据，则跳过该个股，不作为选择
                if (stockStartPosition == -1 || stockEndPosition == -1)
                {
                   
                    continue;
                }
                double[] closePrices = this.container.stocks[tradingStock.code].factors["价格收盘价"];
                double startPrice = closePrices[stockStartPosition - initialStartPosition];
                double endPrice = closePrices[stockEndPosition - initialStartPosition];
                double increase = (closePrices[stockEndPosition - initialStartPosition] - closePrices[stockStartPosition - initialStartPosition]) / closePrices[stockStartPosition - initialStartPosition];
                tradingStock.comparedValue=increase;
                tradingStock.buyDate = tradingDate;
                comparedStocks.Add(tradingStock);
            }
            if (comparedStocks.Count == 0)
                return selectedStocks;

            string sortType = this.paraList["排序"].value.ToString();

            comparedStocks = this.sort(comparedStocks, sortType);

            //开始选择
            string isPercent = this.paraList["是否比例"].value.ToString();
            int selectStartPosition = 0;
            int selectEndPosition = 0;
            if (isPercent == "是")
            {
                int startPercent = Int32.Parse(this.paraList["范围开始"].value.ToString());
                selectStartPosition = Convert.ToInt32(startPercent * 0.01 * comparedStocks.Count);

                int endPercent = Int32.Parse(this.paraList["范围结束"].value.ToString());
                selectEndPosition = Convert.ToInt32(endPercent * 0.01 * comparedStocks.Count);
            }
            else if (isPercent == "否")
            {
                selectStartPosition = Int32.Parse(this.paraList["范围开始"].value.ToString());
                selectEndPosition = Int32.Parse(this.paraList["范围结束"].value.ToString());
            }


            selectedStocks = comparedStocks.GetRange(selectStartPosition, selectEndPosition - selectStartPosition);
            return selectedStocks;
        }

        public override List<TradingStock> sell(List<TradingStock> stockList, string tradingDate)
        {
            int holdPeriod=Int32.Parse( this.paraList["持有时间"].value.ToString());
            int buyPosition = this.container.tradingDates.IndexOf(tradingDate);
            if (buyPosition + holdPeriod >= this.container.tradingDates.Count)
                return new List<TradingStock>();

            string sellDate = this.container.tradingDates[buyPosition + holdPeriod];
            foreach (TradingStock tradingStock in stockList)
            {
                tradingStock.sellDate = sellDate;
                
            }
            return stockList;
        }
    }
}
